package com.che.software.testato.business;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.che.software.testato.business.exception.UserCreationManagerException;
import com.che.software.testato.business.exception.UserSearchManagerException;
import com.che.software.testato.domain.dao.IUserDAO;
import com.che.software.testato.domain.dao.jdbc.exception.UserCreationDAOException;
import com.che.software.testato.domain.dao.jdbc.exception.UserSearchDAOException;
import com.che.software.testato.domain.entity.User;
import com.che.software.testato.domain.entity.creation.UserCreation;
import com.che.software.testato.domain.entity.search.UserSearch;

/**
 * Manager dedicated to the users.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Service("userManager")
public class UserManager {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(UserManager.class);

	/**
	 * Members.
	 */
	@Autowired
	private IUserDAO userDAO;

	/**
	 * Creates an user from his properties bean.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param userToCreateBean the properties to use to create the user.
	 * @throws UserCreationManagerException if an error occurs during the
	 *         creation.
	 */
	public void createUserFromBean(UserCreation userToCreateBean) throws UserCreationManagerException {
		LOGGER.debug("createUserFromBean(" + userToCreateBean.getLogin() + ").");
		try {
			userDAO.createUserFromBean(userToCreateBean);
		} catch (UserCreationDAOException e) {
			throw new UserCreationManagerException(e);
		}
	}

	/**
	 * Reponsable user search.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the resulting object list.
	 * @since July, 2011.
	 * @throws UserSearchManagerException if an error occurs during the search.
	 */
	public List<User> searchResponsableUser() throws UserSearchManagerException {
		LOGGER.debug("searchResponsableUser().");
		try {
			return userDAO.searchResponsableUser();
		} catch (UserSearchDAOException e) {
			throw new UserSearchManagerException(e);
		}
	}

	/**
	 * User search from an id.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param userId the id of the looked for user.
	 * @return the resulting object.
	 * @since July, 2011.
	 * @throws UserSearchManagerException if an error occurs during the search.
	 */
	public User searchUserFromId(int userId) throws UserSearchManagerException {
		LOGGER.debug("searchUserFromId(" + userId + ").");
		List<User> res = searchUsers(new UserSearch(userId));
		return (null != res && !res.isEmpty()) ? res.get(0) : null;
	}

	/**
	 * User search from a bean of criterions.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param searchBean the criterions to use for the search.
	 * @return the resulting object list.
	 * @since July, 2011.
	 * @throws UserSearchManagerException if an error occurs during the search.
	 */
	public List<User> searchUsers(UserSearch searchBean) throws UserSearchManagerException {
		LOGGER.debug("searchUsers().");
		try {
			return userDAO.searchUsers(searchBean);
		} catch (UserSearchDAOException e) {
			throw new UserSearchManagerException(e);
		}
	}

	/**
	 * Getter for the private field value userDAO.
	 * 
	 * @return the userDAO field value.
	 */
	public IUserDAO getUserDAO() {
		return userDAO;
	}

	/**
	 * Setting a value to the userDAO field.
	 * 
	 * @param userDAO the value to set.
	 */
	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}
}